import java.awt.BorderLayout;
import java.awt.Cursor;
import java.awt.EventQueue; 
import java.awt.Frame;
import java.awt.Point;

import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;

import javax.swing.JFrame; 
import javax.swing.JPanel;
import javax.swing.Timer;



public class Main extends JFrame implements PanelListener, ActionListener{ 

	private Printer printer;
	private int print_count=1;
	private int width;
	private int height;
	
	//direction
	// 0 : home
	// 1 : back
	// 2 : forward
	
	private int direction=0;
	private int animation_p=0;
	private int speed=40;
	private Timer timer;
	
	private int ani_count=0;
	
	private int page=0;
	
	private long[] page_value = new long[22];
	/** 
     * Launch the application. 
     */ 
    public static void main(String[] args) { 
        EventQueue.invokeLater(new Runnable() { 
            public void run() { 
                try { 
                	Main frame = new Main(); 
                    frame.setVisible(true); 
                } 
                catch (Exception e) { 
                    e.printStackTrace(); 
                } 
            } 
        }); 
    } 

    /** 
     * Create the frame. 
     */ 
    public Main() {        
    	printer = new Printer();
    	
    	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
        
        Toolkit tk = Toolkit.getDefaultToolkit();  
        width = ((int) tk.getScreenSize().getWidth());  
        height = ((int) tk.getScreenSize().getHeight()); 
        setSize(width, height);
        setExtendedState(Frame.MAXIMIZED_BOTH);
        setUndecorated(true);
        setLocation(-500, -500);
        
        JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout());
        setContentPane(panel);
        
        Intro_Panel intro_panel = new Intro_Panel(width, height);
        intro_panel.setPanelListener(this);
        
        getContentPane().add(intro_panel);
        
        BufferedImage cursorImg = new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB);
        Cursor blankCursor = Toolkit.getDefaultToolkit().createCustomCursor(
        	    cursorImg, new Point(0, 0), "blank cursor");
        
        getContentPane().setCursor(blankCursor);
    } 
    
    @Override
    public void onBack(){
    	--page;
    	switch(page){
    	case 1:
    		P00_Panel p00_panel = new P00_Panel(width, height);
    		p00_panel.setPanelListener(this);
    		p00_panel.setBounds(0, 0, width, height);
        	getContentPane().add(p00_panel);
    		break;
    	case 2:
    		P01_Panel p01_panel = new P01_Panel(width, height);
    		p01_panel.setPanelListener(this);
    		p01_panel.setBounds(0, 0, width, height);
        	getContentPane().add(p01_panel);
    		break;
    	case 3:
    		P02_Panel p02_panel = new P02_Panel(width, height);
    		p02_panel.man=(int)page_value[0];
    		p02_panel.setPanelListener(this);
    		p02_panel.setBounds(0, 0, width, height);
        	getContentPane().add(p02_panel);
    		break;
    	case 4:
    		P03_Panel p03_panel = new P03_Panel(width, height);
    		p03_panel.man=(int)page_value[0];
    		p03_panel.setPanelListener(this);
    		p03_panel.setBounds(0, 0, width, height);
        	getContentPane().add(p03_panel);
    		break;
    	case 5:
    		P04_Panel p04_panel = new P04_Panel(width, height);
    		p04_panel.man=(int)page_value[0];
    		p04_panel.setPanelListener(this);
    		p04_panel.setBounds(0, 0, width, height);
        	getContentPane().add(p04_panel);
    		break;
    	case 6:
    		P05_Panel p05_panel = new P05_Panel(width, height);
    		p05_panel.man=(int)page_value[0];
    		p05_panel.setPanelListener(this);
    		p05_panel.setBounds(0, 0, width, height);
        	getContentPane().add(p05_panel);
    		break;
    	case 7:
    		P06_Panel p06_panel = new P06_Panel(width, height);
    		p06_panel.man=(int)page_value[0];
    		p06_panel.setPanelListener(this);
    		p06_panel.setBounds(0, 0, width, height);
        	getContentPane().add(p06_panel);
    		break;
    	}
    	
    	speed=35;
    	ani_count=0;
    	direction=1;
    	animation_p=0;
    	timer = new Timer(10, this);
    	timer.start();
    	
    }
    
    @Override
    public void onForward(){
    	++page;
    	
    	switch(page){
    	case 1:
    		P00_Panel p00_panel = new P00_Panel(width, height);
    		p00_panel.setPanelListener(this);
    		p00_panel.setBounds(0, 0, width, height);
        	getContentPane().add(p00_panel);
    		break;
    	case 2:
    		P01_Panel p01_panel = new P01_Panel(width, height);
    		p01_panel.setPanelListener(this);
    		p01_panel.setBounds(0, 0, width, height);
        	getContentPane().add(p01_panel);
    		break;
    	case 3:
    		P02_Panel p02_panel = new P02_Panel(width, height);
    		p02_panel.man=(int)page_value[0];
    		p02_panel.setPanelListener(this);
    		p02_panel.setBounds(0, 0, width, height);
        	getContentPane().add(p02_panel);
    		break;
    	case 4:
    		P03_Panel p03_panel = new P03_Panel(width, height);
    		p03_panel.man=(int)page_value[0];
    		p03_panel.setPanelListener(this);
    		p03_panel.setBounds(0, 0, width, height);
        	getContentPane().add(p03_panel);
    		break;
    	case 5:
    		P04_Panel p04_panel = new P04_Panel(width, height);
    		p04_panel.man=(int)page_value[0];
    		p04_panel.setPanelListener(this);
    		p04_panel.setBounds(0, 0, width, height);
        	getContentPane().add(p04_panel);
        	break;
    	case 6:
    		P05_Panel p05_panel = new P05_Panel(width, height);
    		p05_panel.man=(int)page_value[0];
    		p05_panel.setPanelListener(this);
    		p05_panel.setBounds(0, 0, width, height);
        	getContentPane().add(p05_panel);
    		break;
    	case 7:
    		P06_Panel p06_panel = new P06_Panel(width, height);
    		p06_panel.man=(int)page_value[0];
    		p06_panel.setPanelListener(this);
    		p06_panel.setBounds(0, 0, width, height);
        	getContentPane().add(p06_panel);
    		break;
    	case 8:
    		P07_Panel p07_panel = new P07_Panel(width, height);
    		p07_panel.man=(int)page_value[0];
    		p07_panel.setPanelListener(this);
    		p07_panel.setBounds(0, 0, width, height);
        	getContentPane().add(p07_panel);
    		break;
    	case 9:
    		Loading_Panel loading_panel = new Loading_Panel(width, height);
    		loading_panel.setPanelListener(this);
    		loading_panel.setBounds(0, 0, width, height);
        	getContentPane().add(loading_panel);
    		break;
    	}
    	
    	speed=35;
    	ani_count=0;
    	direction=2;
    	animation_p=0;
    	timer = new Timer(10, this);
    	timer.start();
    }
    
    @Override
    public void onHome(){
    	page=0;
    	Intro_Panel intro_panel = new Intro_Panel(width, height);
    	intro_panel.setPanelListener(this);
    	intro_panel.setBounds(0, 0, width, height);
    	getContentPane().add(intro_panel);
    	
    	speed=25;
    	ani_count=0;
    	direction=0;
    	animation_p=0;
    	timer = new Timer(10, this);
    	timer.start();
    }
    
    @Override
    public void onPrint(){
    	printer.print(page_value,print_count);
    	printer.print(page_value,print_count++);
    	if(print_count>999){
    		print_count=0;
    	}
    	page_value = new long[22];
    }
    
    @Override
    public void onChangeValue(int position, long value){
    	page_value[position]=value;
    }
    
    public void actionPerformed(ActionEvent e) {
    	//animation
    	if(direction==0){
    		if(ani_count==33)
				speed=8;
		}else{
			if(ani_count==42){
				speed=10;
			}
		}
    	//System.out.println(ani_count);
    	//54 43
    	++ani_count;
    	
    	animation_p+=speed;
    	
    	if(direction==0){
    		if(animation_p>height){
    			animation_p=height;
    		}
    	}else{
    		if(animation_p>width){
    			animation_p=width;
    		}
    	}
    	
    	
    	switch(direction){
    	case 0:
    		//down
    		getContentPane().getComponent(0).setBounds(0, animation_p, width, height);
    		getContentPane().getComponent(1).setBounds(0, animation_p-height, width, height);
    		break;
    	case 1:
    		//right
    		getContentPane().getComponent(0).setBounds(animation_p, 0, width, height);
    		getContentPane().getComponent(1).setBounds(animation_p-width, 0, width, height);
    		break;
    	case 2:
    		//left
    		getContentPane().getComponent(0).setBounds(-animation_p, 0, width, height);
    		getContentPane().getComponent(1).setBounds(width-animation_p, 0, width, height);
    		break;
    	}
    	
    	if(direction==0){
    		if(animation_p==height){
    			getContentPane().remove(0);
    			timer.stop();
    		}
    	}else{
    		if(animation_p==width){
    			getContentPane().remove(0);
    			timer.stop();
    		}
    	}
    }
}